//
//  TTCustomGoodsConfirmViewController.m
//  TaoTuo
//
//  Created by LG on 2020/11/24.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTCustomGoodsConfirmViewController.h"
#import "TTCustomInfoTableViewCell.h"
#import "TTCustomConfirmFooterView.h"
#import "TTCustomChooseLuoshiView.h"


@interface TTCustomGoodsConfirmViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<TTCustomInfoModel *> *dataArray;
@property (nonatomic, strong) TTCustomHeaderView *headerView;

@property (nonatomic, strong) TTCustomConfirmFooterView *footerView;

@property (nonatomic, strong) UIView *bottomView;

@end

@implementation TTCustomGoodsConfirmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"确认订单";
    self.view.backgroundColor = UIColorHex(#F7F7F7);
    
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(72);
    }];
    
    [self.tableView reloadData];
    
    
}

- (void)setArray:(NSArray<NSDictionary *> *)array
{
    _array = array;
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = array[i];
        TTCustomInfoModel *model = [TTCustomInfoModel setModelWithTitle:dic.allKeys.firstObject content:dic.allValues.firstObject];
        if (i == array.count - 1) {
            model.rectCorner = UIRectCornerBottomLeft | UIRectCornerBottomRight;
        }
        if ([model.content isEqualToString:@"已加急"]) {
            model.contetnColor = UIColorHex(#F7172B);
        }
        
        [self.dataArray addObject:model];
    }
    
    self.tableView.tableFooterView = self.footerView;
    
    self.tableView.tableHeaderView = self.headerView;
    
    [self.tableView reloadData];
    
}

- (void)setHeaderViewWithPrice:(NSString *)price info:(NSString *)info customModel:(nonnull TTCustomConditionModel *)customModel
{
    self.headerView.customModel = customModel;
    self.headerView.price = price;
    self.headerView.infoString = info;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTCustomInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataArray[indexPath.row];
    cell.backgroundColor = self.tableView.backgroundColor;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 10)];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [UILabel labelWithTitle:@"    托定制需求" color:UIColorHex(333333) font:[UIFont boldSystemFontOfSize:15]];
    label.backgroundColor = UIColor.whiteColor;
    label.frame = CGRectMake(0, 0, self.view.width, 40);
    return label;
}

/// 立即购买
- (void)bottomBtnClick
{
    
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = self.view.backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.layer.cornerRadius = 10;
        _tableView.layer.masksToBounds = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 36;
        [_tableView registerClass:[TTCustomInfoTableViewCell class] forCellReuseIdentifier:@"cell"];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_topLayoutGuideBottom).offset(12);
            make.left.mas_equalTo(10);
            make.right.equalTo(self.view).offset(-10);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom).offset(-72);
        }];
    }
    return _tableView;
}

- (TTCustomConfirmFooterView *)footerView
{
    if (!_footerView) {
        _footerView = [[TTCustomConfirmFooterView alloc] initWithFrame:CGRectMake(0, 0, self.view.width-20, 100)];
        /// 从购买商品
        @weakify(self)
        _footerView.fromBuyGoodsChooseBlock = ^{
            @strongify(self)
            TTCustomChooseLuoshiView *luoshiView = [[TTCustomChooseLuoshiView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 557 * kScale)];
            luoshiView.type = QueryGoodsTypeWithOrder;
            [luoshiView show];
        };
        
        /// 从购物车
        _footerView.fromGoodsCartChooseBlock = ^{
            @strongify(self)
            TTCustomChooseLuoshiView *luoshiView = [[TTCustomChooseLuoshiView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 557 * kScale)];
            luoshiView.type = QueryGoodsTypeWithShoppingCart;
            [luoshiView show];
        };
    }
    return _footerView;
}

- (NSMutableArray<TTCustomInfoModel *> *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (TTCustomHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[TTCustomHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 130)];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    return _headerView;
}


- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];    //WithFrame:CGRectMake(0, 0, self.view.width, 72)
        _bottomView.backgroundColor = UIColor.whiteColor;
        
        UIButton *bottomBtn = [UIButton createButtonWithTitle:@"立即购买" titleColor:UIColor.whiteColor titleFont:[UIFont boldSystemFontOfSize:16] backgroundColor:UIColorHex(#8B57E6) cornerRadius:20 target:self action:@selector(bottomBtnClick)];
//        bottomBtn.frame = CGRectMake(15, 20, self.view.width - 30, 40);
        [_bottomView addSubview:bottomBtn];
        [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.left.mas_equalTo(15);
            make.centerX.equalTo(self.bottomView);
            make.height.mas_equalTo(40);
        }];
    }
    return _bottomView;
}

@end
